#include "hhash.h"

#define LOGPATH	"/export/home/stock/log"
MYSQL *conn, mysql;
char gszdate[20];

int fnMySQLInit();
void fnMySQLDisconn();
void daemon_init();

int 
fnMySQLInit()
{
	mysql_init( &mysql );
	if ( ( conn = mysql_real_connect( &mysql, "192.168.0.233",  "stock",  "stov2", "stock_db", 3333, NULL, 0 ) ) == NULL )
	{
		lprintf( log, ERROR ,"%s\n", mysql_error(&mysql));
		return 0;
	}
	else return 1;
}

void 
fnMySQLDisconn( void )
{
	mysql_close( conn );
}

void daemon_init()
{
	time_t tp,t;
        struct tm tmstr;
        char szLog[20], szSQL[100];
        MYSQL_RES *result;
        MYSQL_ROW row;
        int i, num_rows, istock_no, nums=0;
  
        t = time( &tp );
        tmstr = *localtime( &t );
        sprintf(gszdate,"%04d%02d%02d", tmstr.tm_year+1900, tmstr.tm_mon+1, tmstr.tm_mday );
        sprintf( szLog, "%s/rescue.log", LOGPATH );
        
        log=log_open( szLog, 0 );
        SHM_init((key_t) 1101);
	if( fnMySQLInit())
	{
		sprintf( szSQL, " select stock_no,stock_name,stock_rel_no,ref_price,max_price,min_price	from TSE.f1 where rec_date='%s'", gszdate );
		
		if ( ! mysql_real_query( &mysql, szSQL, strlen( szSQL ) ) )
  		{
  			result=mysql_store_result( &mysql );
  			while (( row = mysql_fetch_row( result )))
  			{
  				
  				istock_no=atoi( row[2] ); //Relative arrangement of the stock number
  				sprintf( ltick->istockno[nums], "%d", istock_no); 
  				sprintf( ltick->DIFF_num[istock_no].stockno,"%s", row[0] ); //Stock Code
				sprintf( ltick->DIFF_num[istock_no].stock_name, "%s", row[1] ); //Stock Name
				sprintf( ltick->DIFF_num[istock_no].refprice, "%s", row[3] ); //Stock reference price
				sprintf( ltick->DIFF_num[istock_no].maximum, "%s", row[4] ); //	Stock limit-price
				sprintf( ltick->DIFF_num[istock_no].minimum, "%s", row[5] ); //Stock lower limit price
				nums++;
   			}
   			ltick->total=nums;
   			mysql_free_result( result );
   		}
   		fnMySQLDisconn();
   		printf(" input shm ok !!\n ");
   	}
   	log_close( log );
}

int main()
{
	daemon_init();
	return 0;
}